package com.py.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.py.admin.mapper.AdUserMapper;
import com.py.admin.service.AdUserService;
import com.py.apis.user.IUserClient;
import com.py.model.admin.dtos.AdUserDto;
import com.py.model.admin.pojos.AdUser;
import com.py.model.common.dtos.ResponseResult;
import com.py.model.common.enums.AppHttpCodeEnum;
import com.py.model.user.dtos.AuthDto;
import com.py.utils.common.AppJwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 管理员用户信息表 服务实现类
 * </p>
 *
 * @author itheima
 */
@Slf4j
@Service
@Transactional
public class AdUserServiceImpl extends ServiceImpl<AdUserMapper, AdUser> implements AdUserService {

    @Autowired
    private IUserClient userClient;



    /**
     * 用户登录
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(AdUserDto dto) {
        //参数检查
        if (StringUtils.isEmpty(dto.getName()) || StringUtils.isEmpty(dto.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "用户名和密码不能为空");
        }
        //检查用户名
        LambdaUpdateWrapper<AdUser> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(AdUser::getName, dto.getName());
        AdUser user = getOne(lqw);
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_ERROR);
        }
        //校验密码
        String salt = user.getSalt();
        String password = DigestUtils.md5DigestAsHex(( dto.getPassword()+salt).getBytes());
        if (user.getPassword().equals(password)) {
            //返回token
            Map<String, Object> map = new HashMap<>();
            map.put("token", AppJwtUtil.getToken(user.getId().longValue()));
            //把密码和salt置空,保证安全性
            user.setPassword(null);
            user.setSalt(null);
            map.put("user", user);
            return ResponseResult.okResult(map);
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_ERROR);
        }

    }





    /**
     * 管理端查询审核中的所有用户
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findAll(AuthDto dto) {

        //调用feign
        ResponseResult result = userClient.list(dto);
        //获取数据
        Object data = result.getData();

        return ResponseResult.okResult(data);
    }

    /**
     * 审核驳回
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authFail(AuthDto dto) {
        //校验参数
        dto.checkParam();
        //判断id
        if (dto.getId()!=null){
            //调用feign
            userClient.authFail(dto);

        }

        return null;
    }

    /**
     * 审核通过
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authPass(AuthDto dto) {
        //校验参数
        dto.checkParam();
        //判断id
        if (dto.getId()!=null){
            //feign 调用 userClient修改user表格
            userClient.authPass(dto);


        }

        return null;
    }
}
